package com.scalar.chiptrack.operations.process.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.ProcessTrackDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.ProcessTrack;

public class ProcessTrackAction extends ChipTrackAction{
	
	public ActionForward execute(ActionMapping mapping,
                                   ActionForm    form,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws ScalarException{
		
		String productId = null;
		String productName = "";
		String action = request.getParameter("action");

		if((action != null) && (action.equalsIgnoreCase("true")))
		{
			productId = request.getParameter("productID");
		}
		else
		{
			HttpSession hs = request.getSession(false);
		 	//productId = (String)hs.getAttribute("baseProductId");
		 	
		 	// the below line should be uncommented and next line to be 
		 	// commented for new product setup 
			//productId = (String)request.getAttribute("baseProductID");
			productId = (String)request.getParameter("baseProductID");
			if( productId == null )
			{
				productId = (String)request.getAttribute("baseProductID");
			}
		}
				
		ProcessTrack[] track = null;
		if((productId != null) && !productId.equals(""))
		{
			try
			{
				track = ProcessTrackDAO.getProcessView(Integer.parseInt(productId));
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}

			try 
			{
				productName = ProductDao.getBaseProductName( productId );
			} 
			catch (ScalarException e1) 
			{
				throw new ScalarException ("Could not get Product name");
			}
			
			request.setAttribute("process", track);
			request.setAttribute("baseProductID", productId);
			request.setAttribute( "baseProductName" , productName );
			
			return mapping.findForward("processFlow");
		}/*else{
			System.out.println("Product ID should be null = "+productId);
		}*/
		return null;		
	}
}